<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mcrypt.ciphers.html">Mcrypt ciphers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="up"><a href="book.mcrypt.html">Mcrypt</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mcrypt.examples" class="appendix">
 <h1>范例</h1>

 <p class="para">
  Mcrypt can be used to encrypt and decrypt using the above
  mentioned ciphers. If you linked against <em>libmcrypt-2.2.x</em>, the
  four important mcrypt commands (<span class="function"><a href="function.mcrypt-cfb.html" class="function">mcrypt_cfb()</a></span>,
  <span class="function"><a href="function.mcrypt-cbc.html" class="function">mcrypt_cbc()</a></span>, <span class="function"><a href="function.mcrypt-ecb.html" class="function">mcrypt_ecb()</a></span>,
  and <span class="function"><a href="function.mcrypt-ofb.html" class="function">mcrypt_ofb()</a></span>) can operate in both modes
  which are named <strong><code>MCRYPT_ENCRYPT</code></strong> and
  <strong><code>MCRYPT_DECRYPT</code></strong>, respectively.
 </p>
 <p class="para">
  If you linked against libmcrypt 2.4.x or 2.5.x, these functions are still
  available, but it is recommended that you use the advanced functions.
  <div class="example" id="example-812">
   <p><strong>Example #1 Encrypt an input value with <em>TripleDES</em> under 2.4.x and higher in <em>ECB</em> mode</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"this&nbsp;is&nbsp;a&nbsp;secret&nbsp;key"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Let&nbsp;us&nbsp;meet&nbsp;at&nbsp;9&nbsp;o'clock&nbsp;at&nbsp;the&nbsp;secret&nbsp;place."</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$td&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'tripledes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ecb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$encrypted_data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mcrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$td</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  This example will give you the encrypted data as a string in
  <em>$encrypted_data</em>.  For a full example see
  <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span>.
 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mcrypt.ciphers.html">Mcrypt ciphers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.mcrypt.html">Mcrypt 函数</a></div>
 <div class="up"><a href="book.mcrypt.html">Mcrypt</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
